package com.timo.redis.controller;

import com.timo.redis.model.Person;
import com.timo.redis.server.HashOperationsRedisServer;
import com.timo.redis.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: Person 操作控制器
 * @Author: timo
 * @Date: 2019/8/28 12:18
 */
@Slf4j
@RestController
@RequestMapping("/person")
public class PersonController {

    @Resource
    private HashOperationsRedisServer hashOperationsRedisServer;

    @Resource
    HashOperations<String, String, Object> hashOperations;

    @GetMapping("/save")
    public Result save() {
        Person person = new Person("firstName_1","lastName_1");

        hashOperations.put("two_person", person.hashCode() + "", person);

        return Result.success(hashOperations.get("two_person", person.hashCode()+""));
//        hashOperationsRedisServer.add("one_person",person);


    }

    @GetMapping("/save/list")
    public void saveList() {
        List<Person> list = new ArrayList<>();
        Person person1 = new Person("firstName_1","lastName_1");
        Person person2 = new Person("firstName_2","lastName_2");
        Person person3 = new Person("firstName_3","lastName_3");

        list.add(person1);
        list.add(person2);
        list.add(person3);

        hashOperationsRedisServer.addList("one_person_list", list);
    }


    @GetMapping("/get/{key}")
    public Result get(@PathVariable String key) {
        Person person = (Person) hashOperations.get(key, "56374027");
        Person person1 = hashOperationsRedisServer.get(key, Person.class);
        List<Person> list = new ArrayList<>();
        list.add(person);
        list.add(person1);
        return Result.success(list);
    }

    @GetMapping("/get/list/{key}")
    public Result getList(@PathVariable String key) {
        List list = hashOperationsRedisServer.getList(key, List.class);
        return Result.success(list);
    }
}
